package cbm.commons;

import java.io.IOException;
import java.util.ResourceBundle;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

public class SeguridadFilter implements Filter {
	
	static Logger logger = Logger.getLogger(SeguridadFilter.class.getName());
	private static ResourceBundle rb = ResourceBundle.getBundle("continuidadweb");

	public FilterConfig config;

    public SeguridadFilter() {
        
    }

	public void destroy() {
		
	} 

	public void guardaLog(String mensaje,HttpServletRequest request){
		/*ServletContext servletContext = config.getServletContext();
		ArrayList<String> listException=(ArrayList<String>)servletContext.getAttribute("_LOG_");
		if(listException==null){listException=new ArrayList<String>();}
		listException.add(mensaje);		
		servletContext.setAttribute("_LOG_",listException);
		String tabla="<table width='100%' border='1'>";
		for (int i = 0; i < listException.size(); i++) {
			tabla+="<tr><td width='5%'>"+(i+1)+"</td><td>";			
			tabla+=listException.get(i);
			tabla+="</td></tr>";
		}
		tabla+="</table>";
		servletContext.setAttribute("_LOG_HTML",tabla);*/
	}
	
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		
		logger.info("----------------------FILTRO SEGURIDAD---------------");
		guardaLog("----------------------FILTRO SEGURIDAD---------------", (HttpServletRequest)request);
		try{
			HttpServletRequest req = (HttpServletRequest)request;
			//HttpServletResponse resp = (HttpServletResponse)response;
			HttpSession session = req.getSession();
			String uri       = req.getRequestURI();
			String login     = rb.getString("login");
			String pagLogin  = rb.getString("pagina_login");
			String pagExpira = rb.getString("pagina_expira");
			SessionContainer sc = null;
			guardaLog("uri : "+uri, req);
			guardaLog("login : "+login, req);
			guardaLog("pagLogin : "+pagLogin, req);
			guardaLog("pagExpira : "+pagExpira, req);
			
			
			
		/*	if(!uri.endsWith(login) && !uri.endsWith("loginlogout.do") && !uri.endsWith("acceso.jsp")&& !uri.endsWith("/CONNEG/") && !uri.endsWith("Expirado.jsp") && !uri.endsWith("Login.jsp") && !uri.endsWith("Principal.jsp")&& !uri.endsWith("log.jsp")&& !uri.endsWith("editorHTML.jsp")&& !uri.endsWith("PruebaHTML.jsp")){
				//Se valida si el usuario esta creado en la session
				sc = (SessionContainer)session.getAttribute(ConstantsWeb.SESSION_CONTAINER);
				guardaLog("SessionContainer : "+sc, req);
				if(sc == null){
					//req.getRequestDispatcher(pagLogin).forward(request,response);
					guardaLog("forward : "+pagExpira, req);
					req.getRequestDispatcher(pagExpira).forward(request,response);
				} else {
					guardaLog("USUARIO : "+sc.getSessionElement(ConstantsWeb.USUARIO), req);
					if(sc.getSessionElement(ConstantsWeb.USUARIO)== null){
						guardaLog("forward : "+pagExpira, req);
						req.getRequestDispatcher(pagExpira).forward(request,response);
					}else{
						guardaLog("1 chain.doFilter(request, response)", req);
						chain.doFilter(request, response);
					}
				}
			}else{*/
				guardaLog("2 chain.doFilter(request, response)", req);
				chain.doFilter(request, response);
			//}
		}catch(IOException e){
			guardaLog(e.getMessage(), (HttpServletRequest)request);
			e.printStackTrace();logger.error(e);
			throw e;
		}catch(ServletException e){
			guardaLog(e.getMessage(), (HttpServletRequest)request);
			e.printStackTrace();logger.error(e);
			throw e;
		}catch(Exception e){
			guardaLog(e.getMessage(), (HttpServletRequest)request);
			e.printStackTrace();logger.error(e);
		}
		
	}

	public void init(FilterConfig fConfig) throws ServletException {
		this.config = fConfig;
	}
}
